<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */

namespace App\Http\Controllers\Admin;

use App\Models\StudentInfo;
use App\Traits\AdminHelp;
use Carbon\Carbon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Medz\IdentityCard\China\Identity;

class StudentInfoController extends AdminController
{

    use AdminHelp;

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '学籍信息管理';


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new StudentInfo());

        $grid->column('id', 'ID')->sortable();
        $grid->column('name', '姓名');
        $grid->column('sex', '性别')->display(function () {
            $info = new Identity($this->sid);
            $sex = $info->gender();

            return $sex ?? '未知';
        });
        $grid->column('age', '年龄')->display(function () {
            $info = new Identity($this->sid);
            $birthday = new Carbon($info->birthday());

            return $birthday->diffInYears() ?? '未知';
        });
        $grid->column('sid', '身份证号');
        $grid->column('birthday', '出生年月')->display(function () {
            $info = new Identity($this->sid);
            $birthday = $info->birthday();

            return $birthday ?? '未知';
        });
        $grid->column('city', '所在城市')->display(function () {
            $info = new Identity($this->sid);
            $city = $info->region()->treeString(' ');

            return $city ?? '未知';
        });
        $grid->column('louhao', '所住楼号')->sortable();
        $grid->column('fangjian', '所住寝室');
        $grid->column('legal', '身份证是否合法')->display(function () {
                $info = new Identity($this->sid);
                $city = $info->legal();

                return $city ? '合法' : '不合法';
            });
        $grid->column('starttime', '创建时间');

        $grid->expandFilter();

        $grid->setActionClass(Grid\Displayers\Actions::class);

        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('name', '姓名');
            $filter->equal('sid', '身份证号');
        });

        return $grid;
    }


    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        //$show = new Show(StudentInfo::query()->findOrFail($id));

        //$show->field('id', 'ID');
        //$show->field('user_id', '用户');
        //$show->field('nickname', '昵称');
        //$show->field('email', '邮箱');
        //$show->field('avatar', '头像');
        //$show->field('original', '详细信息');
        //$show->field('token', 'Token');
        //$show->field('provider', '来源');
        //$show->field('admin_user_id', '操作人');
        //$show->field('created_at', '创建时间');
        //$show->field('updated_at', '修改时间');

        //return $show;
    }
}
